package com.java.util;

import com.java.util.pojo.CountPO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 将集合基于某个属性分组，并计算对应的List<T>的size大小
 */
public class CalCountToMapFromList {

    public static void main(String[] args) {

        List<CountPO> countPOList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            if (i < 5) {
                CountPO build = CountPO.builder()
                        .name(i + "_name")
                        .hoteId(10).build();
                countPOList.add(build);
            }
            else if( i < 10){
                CountPO build = CountPO.builder()
                        .name(i + "_name")
                        .hoteId(20).build();
                countPOList.add(build);
            }
            else if(i < 20 ){
                CountPO build = CountPO.builder()
                        .name(i + "_name")
                        .hoteId(30).build();
                countPOList.add(build);
            }
        }


        Map<Integer, Long> collect = countPOList
                .stream()
                .collect(Collectors.groupingBy(
                        CountPO::getHoteId,
                        Collectors.counting()
                ));


        Map<Integer, Integer> collect1 = countPOList.stream()
                .collect(Collectors.groupingBy(
                        CountPO::getHoteId,
                        Collectors.collectingAndThen(Collectors.toList(), List::size)
                ));

        System.out.println(collect);

        System.out.println(collect1);
    }
}
